﻿@using MvcContrib.UI.Grid
@using XRM.Security.DataAccess.Enums
@using XRM.Security.DataAccess.Helpers
@using XRM.Security.WebUI.Consts
@model XRM.Security.WebUI.Models.CertRegister.CertRegisterListModel
@{
    ViewBag.Title = "Реестр сертификатов";
    ViewBag.HideHeader = true;
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div>
    <div>
        @using (Html.BeginForm("Index", "CertRegister", FormMethod.Post, new { @class = "form-horizontal form-horizontal-2", id = "filterForm" }))
        {
            <div class="form-group">
                @Html.LabelFor(m => m.TextFilter, new { @class = "control-label" })
                <div class="control-group">
                    <div class="col-sm-5">
                        <div class="input-group">
                            @Html.TextBoxFor(m => m.TextFilter, new { @class = "form-control", title = "Введите ФИО пользователя или серийный номер сертификата" })
                            @Html.Partial("ClearButtonPartial")
                        </div>
                    </div>
                    <div class="col-sm-1">
                        <button class="btn btn-primary btn-block" type="submit" id="btnFind">Найти</button>
                    </div>
                </div>
            </div>
        
            <div class="form-group">
                @Html.LabelFor(m => m.ExpireDayFilter, new { @class = "control-label" })
                <div class="control-group">
                    <div class="col-sm-2" style="width: 100px;">
                        <div class="control-group input-group">
                            @Html.TextBoxFor(m => m.ExpireDayFilter, new { @class = "form-control integer" })
                            @Html.Partial("ClearButtonPartial")
                        </div>    
                    </div>
                    <div class="col-sm-1" style="width: 60px;">
                        <label class="control-text">дней</label>
                    </div>
                    <div class="col-sm-5">
                        <div class="checkbox">
                            <label for="ShowAllFilter">
                                @Html.CheckBoxFor(x => x.ShowAllFilter)
                                Искать среди недействующих
                            </label>
                        </div>    
                    </div>
                </div>
            </div>
            <div class="form-group">
                @Html.Label(" ", new { @class = "control-label" })
                @Html.ValidationMessageFor(m => m.ExpireDayFilter)
            </div>
        }
    </div>
    
    <hr/>
    
    
    <form id="addCertForm" action="@Url.Action("Add")" method="POST">
        <div class="row">
            <div class="control-group">
                <div class="col-sm-offset-3 col-sm-6">
                    @Html.Partial("CertUploader", new ViewDataDictionary { { ViewDataConsts.Callback, "onCertUpload" } })
                </div>
            </div>
        </div>
    </form>
</div>
    @{
        if (Model.CertList.Any())
        {
            <hr />
            Html.Grid(Model.CertList).Columns(column =>
            {
                column.For(item => Html.ActionLink(item.UserLastName + " " + item.UserFirstName + " " + item.UserMiddleName, "Edit", "UserRegister", new { id = item.UserId }, null))
                    .Named( "Пользователь" ).Attributes();
                column.For(item => item.SerialNumberEtoken).Named("Серийный номер etoken");
                column.For(item => item.SerialNumber).Named("Серийный номер сертификата");
                column.For(item => item.IdentityCenterName).Named("УЦ");
                column.Custom(@<text>
                                       @foreach (var infoSystem in item.InfoSystems)
                                       {
                                           <div>@infoSystem</div>
                                       }
                                   </text>).Named("Информационные системы");
                column.For( item => item.PCSerialNumber ).Named( "Серийный номер ПК" );
                column.Custom(@<text>
                                <input class="hidden" type="text" value="@item.ToDate.ToString("yyyy.MM.dd")" />
                                @item.Period
                                </text>).Named("Срок действия");
                column.Custom(@<text>
                                   <i class="fa fa-action @item.StatusIcon" title="@item.Status.Description()" rel="tooltip"></i>
                                   <span class="hide">@item.Status.Description()</span>
                               </text>).Named( "Статус" ).Attributes( style => "min-width: 60px; text-align: center;" );

                column.Custom
                        (@<text>
                                  <a href="@Url.Action("Edit", new { id = item.Id })" title="Редактировать"> <i class="fa fa-pencil fa-action"></i></a>
                                  <a href="@Url.Action("Download", new { id = item.Id })" title="Скачать"> <i class="fa fa-download fa-action"></i></a>
                              </text>).Named("").Attributes(nowrap => "nowrap");


            }).Attributes(@class => "table table-bordered", id => "table_id")
                .RowAttributes(item => new MvcContrib.Hash(@class => item.Item.RowStyle))
                .Render();
        }
        else if (Model.CertList != null)
        {
            <strong>Сертификаты не найдены.</strong>
        }
    }

@section History
{
    <div class="panel panel-default">
        <div class="panel-heading">
            <i class="fa fa-list-alt fa-fw"></i>
            История
        </div>
        <div class="panel-body" id="historyDiv">
            @{Html.RenderAction("History", new { page = 1 });}    
            @Html.Partial("LoadHistoryButton")
        </div>
    </div>
}

@section scripts
{
     @Scripts.Render("~/bundles/gridlibrary")

    <script type="text/javascript" src="@(Url.Content("~/Scripts/FileUploader.js"))"></script>

    <script type="text/javascript">
        $(document).ready(function () {
            @if (Model.CertList.Any())
            {
                <text>
                $('#table_id').dataTable({
                    paging: false,
                    bFilter: false,
                    bSort: true,
                    info: false,
                    "order": [[0, "asc"]],
                    "columnDefs": [{ "orderDataType": "dom-text", targets: 6 }, { 'bSortable': false, 'aTargets': [1, 2, 4, 6, 8] }]
                });
                </text>
            }
        });

        function onCertUpload() {
            $("#addCertForm").submit();
        }
    </script>
}